package com.techysky.repository;

import java.io.Serializable;
import java.util.List;
import java.util.Map;

import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;

import com.techysky.entity.DataModule;

public interface DataModuleRepository extends CoreRepository<DataModule, Serializable>,Serializable {	
	
	/**
	 * 
	 * @param id
	 * @return
	 */
	List<DataModule> findByPublicModuleId(String id);
	
	/**
	 * 
	 * @param dmCode
	 * @return
	 */
	DataModule findByDmCode(String dmCode);
	
	/**
	 * 
	 * @param code
	 * @param title
	 * @param verNo
	 * @param verType
	 * @param dmCode
	 */
	@Modifying
	@Query(value="update DataModule dm set dm.code=?1,dm.dmTitle=?2,dm.title=?2,dm.verNo=?3,dm.verType=?4 where dm.dmCode=?5")
	public void update(String code, String title, String verNo,String verType,String dmCode);
	
	/**
	 * 
	 * @param pmcode
	 * @return
	 */
	@Query("select distinct new map( p.modelIdentCode as modelIdentCode) from DataModule p where p.pmCode =?1 and p.modelIdentCode is not null order by p.modelIdentCode")
	List<Map<String,Object>> getModelIdentCodeList(String pmcode);
	
	/**
	 * 
	 * @param pmcode
	 * @param modelIdentCode
	 * @return
	 */
	@Query("select distinct new map( p.systemCode as systemCode) from DataModule p where p.pmCode =?1 and p.modelIdentCode=?2 and p.systemCode is not null order by p.systemCode")
	List<Map<String,Object>> getSystemCodeList(String pmcode,String modelIdentCode);
	
	/**
	 * 
	 * @param pmcode
	 * @param modelIdentCode
	 * @param systemCode
	 * @param subSystemCode
	 * @return
	 */
	@Query("select distinct new map( p.assyCode as assyCode) from DataModule p where p.pmCode =?1 and p.modelIdentCode=?2 and p.systemCode=?3 and concat(p.subSystemCode,p.subSubSystemCode)=?4 and p.assyCode is not null order by p.assyCode")
	List<Map<String,Object>> getAssyCodeList(String pmcode,String modelIdentCode,String systemCode,String subSystemCode);
	
	/**
	 * 
	 * @param pmcode
	 * @param modelIdentCode
	 * @param systemCode
	 * @param subSystemCode
	 * @param assyCode
	 * @return
	 */
	@Query("select distinct new map( p.infoCode as infoCode) from DataModule p where p.pmCode =?1 and p.modelIdentCode=?2 and p.systemCode=?3 and concat(p.subSystemCode,p.subSubSystemCode)=?4 and p.assyCode=?5 and p.infoCode is not null order by p.infoCode")
	List<Map<String,Object>> getInfoCodeList(String pmcode,String modelIdentCode,String systemCode,String subSystemCode,String assyCode);
    
	/**
	 * 
	 * @param dmCode
	 * @return
	 */
	List<DataModule> findByDmCodeLike(String dmCode);
	
	/**
	 * 
	 * @param dmCode
	 * @return
	 */
	List<DataModule> findByDmCodeAndPublicModuleIdIsNotNull(String dmCode);
	
	/**
	 * 
	 * @param id
	 * @return
	 */
	public List<DataModule> findByProjectId(String id);

	@Query("select distinct new map( concat(p.subSystemCode,p.subSubSystemCode) as subSystemCode) from DataModule p where p.pmCode =?1 and p.modelIdentCode=?2 and p.systemCode=?3 and p.subSystemCode is not null and p.subSubSystemCode is not null order by p.subSystemCode")
	List<Map<String,Object>> getSubSystemCodeList(String pmcode,String modelIdentCode,String systemCode);

}
